/**
 *
 * @author 
 *
 */
class ItemView extends eui.Component{
    
    private i1: eui.Image;
    private i2: eui.Image;
    private i3: eui.Image;
    private i1_bg: eui.Image;
    private i2_bg: eui.Image;
    private i3_bg: eui.Image;
    public mainIten: eui.Image;
    private imagearr:eui.Image[] = [];
    
    public  audioSoundChannel: egret.SoundChannel;
    private  audio: egret.Sound = new egret.Sound();
    
    
	public constructor() {
    	  super();
        this.skinName = "resource/skins/ItemViewSkin.exml";
	}
	
    public createChildren() {
        super.createChildren();
    }
    
    public switchIma() {
        this.imagearr = [this.i1_bg,this.i2_bg,this.i3_bg];
        var a = Math.random();
        if(a <= 0.33) {
            this.mainIten = this.i1_bg;
            this.i1.source = RES.getRes("view_zm");
            this.i2.source = RES.getRes("view_fm");
            this.i3.source = RES.getRes("view_fm");
        } else if(a > 0.33 && a <= 0.66) {
            this.mainIten = this.i2_bg;
            this.i1.source = RES.getRes("view_fm");
            this.i2.source = RES.getRes("view_zm");
            this.i3.source = RES.getRes("view_fm");
        } else {
            this.mainIten = this.i3_bg;
            this.i1.source = RES.getRes("view_fm");
            this.i2.source = RES.getRes("view_fm");
            this.i3.source = RES.getRes("view_zm");
        }
        for(var i:number = 0;i<this.imagearr.length;i++){
            if(!this.imagearr[i].hasEventListener(egret.TouchEvent.TOUCH_BEGIN)){
                this.imagearr[i].addEventListener(egret.TouchEvent.TOUCH_BEGIN,move,this);
            }
        }
        function move(e: egret.TouchEvent) {
            if( this.y > 356){
                //下落事件
                var _self = this;
                if(e.target == this.mainIten ) {
                    Data.grade++;
                    e.currentTarget.source = "djh";
                    if(this.audioSoundChannel) { this.audioSoundChannel.stop(); }
                    this.audio = RES.getRes("type_mp3");
                    this.audioSoundChannel = this.audio.play(0,1);
                    if(!Data.isrun){
                        Data.isrun = true;
                        GameScene.getInstance().timer1.source = "5_03";
                        GameScene.getInstance().timer2.source = "9_03";
                        GameScene.getInstance().timer.start();
                    }
                    for(var i = 0;i < GameScene.getInstance().itemArr.length;i++) {
                        if(GameScene.getInstance().itemArr[i].y > 534) {
                            GameScene.getInstance().itemArr[i].y = 0;
                            GameScene.getInstance().itemArr[i].mainIten.source = "mdj";
                            GameScene.getInstance().itemArr[i].switchIma();
                        }
                        egret.Tween.get(GameScene.getInstance().itemArr[i]).to({ y: GameScene.getInstance().itemArr[i].y + 178 },100).call(() => {
//                            Data.canTouch = true;
                            if(_self.mainIten.hasEventListener(egret.TouchEvent.TOUCH_BEGIN,move,_self)) {
                                _self.mainIten.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,move,_self);
                            }
                        },_self);
                    }
                } else {
                    //游戏结束
                    if(this.audioSoundChannel) { this.audioSoundChannel.stop(); }
                    this.audio = RES.getRes("nima_mp3");
                    this.audioSoundChannel = this.audio.play(0,1);
                    if(GameScene.getInstance().timer) {
                        GameScene.getInstance().timer.stop();
                        GameScene.getInstance().timer.removeEventListener(egret.TimerEvent.TIMER,GameScene.getInstance().timerFunc,this);
                        GameScene.getInstance().timer.removeEventListener(egret.TimerEvent.TIMER_COMPLETE,GameScene.getInstance().timeOver,this);
                        GameScene.getInstance().timer = null;
                    }
                    if(Main.getInstance().contains(GameOverView.getInstance())) {

                    } else {
                        Main.getInstance().addChild(GameOverView.getInstance());
                        GameOverView.getInstance().lb_results1.text = "您在规定的时间内找到了" + Data.grade.toString() + "部手机";
                        var inp: InputPMView = new InputPMView();
                        Main.getInstance().addChild(inp);
                        Data.isrun = false;
                    }
                }
            }
        }
    }
}
